<?php

namespace app\v1\controller;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public function initialize()
    {
        if (headers_sent() === false) {
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Expose-Headers:Content-Type, X-SMS-Session');
            header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept');
        }

        if (input('?param.user_token')) {
            $this->AutoLogin(input('param.user_token'));
        }
        
        if (request()->isOptions()) {
            exit;
        }

    }

    public function AutoLogin($token)
    {
        $rsa = new \RSA\RSA();
        $user_id = $rsa->Decode($token);

        if ($user_id != null) {
            $GLOBALS['user_status'] = 1;
            $GLOBALS['user_id'] = $user_id;
        }
    }
    
    protected function DevLogin($userid)
    {
        $this->AutoLogin((new \RSA\RSA())->Encode($userid));
    }
}
